﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using myMvcBlog.Models;

namespace myMvcBlog.Areas.Admin.Controllers
{
    public class PostController : AdminBaseController
    {
        [Authorize]
        public ActionResult Delete(long postId)
        {
            var post = Post.SingleOrDefault(p => p.ID == postId);
            post.Delete();

            return RedirectToAction("List");
        }

        [Authorize]
        public ActionResult List(int? page)
        {
            var post = Post.All().OrderByDescending(p => p.PostedTime);

            ListView<Post> postListView = new ListView<Post>();
            postListView.List = post.Skip((page ?? 0) * ItemsPerPage * 2).Take(ItemsPerPage * 2).ToList();

            PagerView pager = new PagerView();
            pager.PageCount = (int)Math.Ceiling((double)post.Count() / (ItemsPerPage * 2));
            pager.CurrentPage = page ?? 0;
            pager.ActionName = "List";
            pager.ControllerName = "Post";

            postListView.Pager = pager;
            return View(postListView);
        }

        public ActionResult PostsByCategory(long categoryId, int? page)
        {
            ListView<Post> lvPosts = new ListView<Post>();
            lvPosts.Title = "Posts under :" + Category.SingleOrDefault(c => c.ID == categoryId).Name;
            var postIds = from pc in PostCategory.All()
                          where pc.CategoryID == categoryId
                          select pc.PostID;

            var posts = (from p in Post.All()
                         join pid in postIds on p.ID equals pid
                         select p).OrderByDescending(p => p.PostedTime);

            lvPosts.List = posts.Skip((page ?? 0) * ItemsPerPage * 2).Take(ItemsPerPage * 2).ToList();
            PagerView pager = new PagerView();
            pager.PageCount = (int)Math.Ceiling((double)posts.Count() / (ItemsPerPage * 2));
            pager.CurrentPage = page ?? 0;
            pager.ActionName = "PostsByCategory";
            pager.ControllerName = "Post";

            lvPosts.Pager = pager;
            return View(lvPosts);
        }


        [HttpGet]
        [Authorize]
        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        [Authorize]
        [ValidateInput(false)]
        public ActionResult Add(Post post, string category)
        {
            post.Save();

            //PostCategory pt = new PostCategory();
            //pt.PostID = post.ID;
            //pt.CategoryID = long.Parse(category);
            //pt.Save();

            return RedirectToAction("List");
        }

        [HttpGet]
        [Authorize]
        public ActionResult Edit(long postId)
        {
            Post post = Post.All().SingleOrDefault(p => p.ID == postId);
            return View(post);
        }

        [Authorize]
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(Post post, string category)
        {
            Post p = Post.SingleOrDefault(po => po.ID == post.ID);
            if (p != null)
            {
                p.Name = post.Name;
                p.UrlName = post.UrlName;
                p.Excerpt = post.Excerpt;
                p.Content = post.Content;
                p.IsPublished = post.IsPublished;
                p.Save();

                var pts = PostCategory.All().Where(pt => pt.PostID == post.ID);
                
                if(pts.Count() > 0 )
                    pts.ToList().ForEach(pt => pt.Delete());

                PostCategory postCategory = new PostCategory()
                {
                    PostID = post.ID,
                    CategoryID = long.Parse(category)
                };

                postCategory.Save();
            }

            return RedirectToAction("Edit");
        }

    }
}
